﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace VersionManage.Common
{
    public class UrlDecode
    {
        #region url解码

        /// <summary>
        /// url解码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DecodeUrl(string value)
        {
            value = System.Web.HttpUtility.UrlDecode(value);
            value = System.Web.HttpUtility.UrlDecode(value.Replace(@"\u", "%u"));
            return value;
        }

        /// <summary>
        /// url解码forsign
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string DecodeUrlForSign(string value)
        {
            value = System.Web.HttpUtility.UrlDecode(value);
            return value;
        }

        /// <summary>
        /// 神州付解密
        /// </summary>
        /// <returns></returns>
        public static string DesDecode(string str, string key)
        {
            byte[] strbyte = Encoding.UTF8.GetBytes(str);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Key = Convert.FromBase64String(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(strbyte, 0, strbyte.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// 神州付加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string DesEncode(string str, string key)
        {
            byte[] strbyte = Encoding.UTF8.GetBytes(str);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.Mode = CipherMode.ECB;
            des.Key = Convert.FromBase64String(key);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(strbyte, 0, strbyte.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }

        #endregion
    }
}
